package com.app.DB;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Types;


public class Customers {
	
	public Customers() {
		PacketName = CustomerName = CustomerIdentity = CustomerPhonenumber = CustomerPassword = CustomerTimeCount = "";
	 }	
    public Customers(
            String OID,
            String UsrOpt,
            String IsActive,
            String IsDeleted,
            String PacketName,
            String CustomerName,
            String CustomerIdentity,
            String CustomerPhonenumber,
            String CustomerPassword,
            String CustomerTimeCount
            
            )
    
   {
        this.OID 					= OID;
        this.UsrOpt 				= UsrOpt;
        this.IsActive 				= IsActive;
        this.IsDeleted 				= IsDeleted;
        this.PacketName 			= PacketName;
        this.CustomerName 			= CustomerName;
        this.CustomerIdentity 		= CustomerIdentity;
        this.CustomerPhonenumber 	= CustomerPhonenumber;
        this.CustomerPassword 		= CustomerPassword;
        this.CustomerTimeCount		= CustomerTimeCount;

    }
    //-------------------- Variable -------------------------
    private String OID,
    		UsrOpt,
    		IsActive,
    		IsDeleted,
    		PacketName,
    		CustomerName,
    		CustomerIdentity,
    		CustomerPhonenumber,
    		CustomerPassword,
    		CustomerTimeCount;
            
    
    Connect con = new Connect(); 
    Connection conn = con.Connect();
    ResultSet rs = null;
   

    // -------------------- GET-SET -------------------------
    
 
	public String getOID() {
		return OID;
	}
	public String getPacketName() {
		return PacketName;
	}
	public void setPacketName(String packetName) {
		PacketName = packetName;
	}
	public String getCustomerName() {
		return CustomerName;
	}
	public void setCustomerName(String customerName) {
		CustomerName = customerName;
	}
	public String getCustomerIdentity() {
		return CustomerIdentity;
	}
	public void setCustomerIdentity(String customerIdentity) {
		CustomerIdentity = customerIdentity;
	}
	public String getCustomerPassword() {
		return CustomerPassword;
	}
	public void setCustomerPassword(String customerPassword) {
		CustomerPassword = customerPassword;
	}
	public String getCustomerTimeCount() {
		return CustomerTimeCount;
	}
	public void setCustomerTimeCount(String customerTimeCount) {
		CustomerTimeCount = customerTimeCount;
	}
	public void setOID(String oID) {
		OID = oID;
	}
	public String getUsrOpt() {
		return UsrOpt;
	}
	public void setUsrOpt(String usrOpt) {
		UsrOpt = usrOpt;
	}
	public String getIsActive() {
		return IsActive;
	}
	public void setIsActive(String isActive) {
		IsActive = isActive;
	}
	public String getIsDeleted() {
		return IsDeleted;
	}
	public void setIsDeleted(String isDeleted) {
		IsDeleted = isDeleted;
	}
	public String getCustomerPhonenumber() {
		return CustomerPhonenumber;
	}
	public void setCustomerPhonenumber(String customerPhonenumber) {
		CustomerPhonenumber = customerPhonenumber;
	}

	//-------------------- SQL ------------------------
    public ResultSet selectCustomers(String StrPacketName,String StrCustomerName,String StrCustomerPassword) throws Exception {
        String StrSql = "SELECT [OID],"
                + "[LastModified],"
                + "[ModifiedDate],"
                + "[ModifiedTime],"
                + "[ModifiedBy],"
                + "[UserModified],"
                + "[CreatDate],"
                + "[CreatTime],"
                + "[CreatedBy],"
                + "[UserCreate],"
                + "[IsActive],"
                + "[IsDeleted],"
                
                + "[PacketName],"
                + "[CustomerName],"
                + "[CustomerIdentity],"
                + "[CustomerPhonenumber],"
                + "[CustomerPassword],"
                + "[CustomerTimeCount] "
                
  				+ "FROM [MegaAutoCare].[dbo].[udfCustomers]() "
  
                + "WHERE [IsDeleted]='0' "; 

        
        if (!StrPacketName.equals("")) {
            StrSql += " AND [PacketName] LIKE '%" + StrPacketName + "%' ";
        }
        
        if (!StrCustomerName.equals("")) {
            StrSql += " AND [CustomerName] LIKE '%" + StrCustomerName + "%' ";
        }
        
        if (!StrCustomerPassword.equals("")) {
            StrSql += " AND [CustomerPassword] LIKE '" + StrCustomerPassword + "' ";
        }

//        if (!CustomernameSearch.equals("")) {
//            StrSql += " AND [CustomerFirstName] LIKE '%" + CustomernameSearch + "%' OR [CustomerPhonenumber] LIKE '%" + CustomernameSearch + "%' " ;
//        }

        //return sqlsel.getReSultSQL(StrSql);
        return con.selectData(StrSql);
    }

   

    public String OperationData(String OperationType) throws Exception {
        try {
        		conn = con.Connect();
                java.sql.CallableStatement cstmt = conn.prepareCall("{call stpCustomersManagement(?,?,?,?,? ,?,?,?,?,? ,?,?)}");
                
                cstmt.setString(1, OperationType);
                cstmt.setString(2, getOID());
                cstmt.setString(3, getUsrOpt());
                cstmt.setString(4, getIsActive());
                cstmt.setString(5, getIsDeleted());

                cstmt.setString(6, getPacketName());
                cstmt.setString(7, getCustomerName());
                cstmt.setString(8, getCustomerIdentity());
                cstmt.setString(9, getCustomerPhonenumber());
                cstmt.setString(10, getCustomerPassword());
                cstmt.setString(11, getCustomerTimeCount());
                
                cstmt.registerOutParameter(12, Types.VARCHAR);
                cstmt.execute();
                return cstmt.getString(12);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
        return null;
    }
}
